package com.orhanobut.logger;

import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Locale;

import static com.orhanobut.logger.Utils.checkNotNull;

/**
 * LogcatLogStrategy is a LogCat implementation for {@link LogStrategy}
 * <p>
 * This simply prints out all logs to Logcat by using standard {@link HiLog} class.
 *
 * @since 2020-11-19
 */
public class LogcatLogStrategy implements LogStrategy {
    static final String DEFAULT_TAG = "NO_TAG";
    static final String TAG_LOG = "[PRETTY_LOGGER] ";
    static final int DOMAIN_ID = 0xD000F00;
    static final HiLogLabel LABEL_LOG = new HiLogLabel(3, DOMAIN_ID, TAG_LOG);
    static final String LOG_FORMAT = "%s: %s";

    @Override
    public void log(int priority, @Nullable String tag, @NotNull String message) {
        checkNotNull(message);
        if (tag == null) {
            // If no log tag is passes then use the default tag else use the tag provided in method parameter
            tag = DEFAULT_TAG;
        }
        switch (priority) {
            case 3:
                HiLog.debug(LABEL_LOG, String.format(Locale.ROOT, LOG_FORMAT, tag, message));
                break;
            case 4:
                HiLog.info(LABEL_LOG, String.format(Locale.ROOT, LOG_FORMAT, tag, message));
                break;
            case 5:
                HiLog.warn(LABEL_LOG, String.format(Locale.ROOT, LOG_FORMAT, tag, message));
                break;
            case 6:
                HiLog.error(LABEL_LOG, String.format(Locale.ROOT, LOG_FORMAT, tag, message));
                break;
            case 7:
                HiLog.fatal(LABEL_LOG, String.format(Locale.ROOT, LOG_FORMAT, tag, message));
                break;
            default:
                HiLog.info(LABEL_LOG, String.format(Locale.ROOT, LOG_FORMAT, tag, message));
        }
    }
}


